package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.InvoiceTitle;

import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.InvoiceTitleMapper;
import com.api.syh56.shengyunhui.service.InvoiceTitleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InvoiceTitleServiceImpl extends ServiceImpl<InvoiceTitleMapper,InvoiceTitle> implements InvoiceTitleService {
    @Autowired
    private InvoiceTitleMapper iTitleMapper;

    @Override
    public void addNewInvoiceTitle(InvoiceTitle invoiceTitle) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        invoiceTitle.setUid(uid);
        //新插入地址如果设为默认
        if(invoiceTitle.getIsDefault()){
            setAlltoDefault(uid);
            save(invoiceTitle);
        } else {
            save(invoiceTitle);
        }
    }


    @Override
    public void removeInvoiceTitle(InvoiceTitle invoiceTitle) {

        Long id = invoiceTitle.getId();
        InvoiceTitle invoiceTitleDB = getById(id);
        //如果删除的car是默认车辆
        if(invoiceTitleDB.getIsDefault()){
            QueryWrapper<InvoiceTitle> wrapper = new QueryWrapper<>();
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            wrapper.lambda().eq(InvoiceTitle::getUid,uid);
            List<InvoiceTitle> addressList = list(wrapper);
            //如果车辆簿还有其他车辆，将其设置为默认，再删除选中车辆
            if(!addressList.isEmpty()){
                setDefault(addressList.get(0).getId());
            }
        }
        removeById(id);
    }


    @Override
    public Page<InvoiceTitle> selectPageByuid(Long uid, Integer pageSize, Integer pageNum) {
        Page<InvoiceTitle> page = new Page<>(pageNum,pageSize);
        QueryWrapper<InvoiceTitle> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(InvoiceTitle::getUid,uid);
        Page<InvoiceTitle> routesPage = iTitleMapper.selectPage(page, wrapper);
        return routesPage;
    }

    @Override
    public void updateInvoiceTitle(InvoiceTitle invoiceTitle) {
        if(invoiceTitle.getIsDefault()){
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            setAlltoDefault(uid);
            updateById(invoiceTitle);
        } else {
            updateById(invoiceTitle);
        }

    }


    public void setAlltoDefault(Long uid){
        QueryWrapper<InvoiceTitle> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(InvoiceTitle::getUid,uid);
        List<InvoiceTitle> lists = list(wrapper);
        lists.forEach(item->{
            item.setIsDefault(false);
        });
        updateBatchById(lists);
    }

    @Override
    public void setDefault(Long id) {
        InvoiceTitle invoiceTitle = getById(id);
        invoiceTitle.setIsDefault(true);
        updateById(invoiceTitle);
    }

}
